
$(function () {
    
    getShop();
    getArea();
    getProduct();

    $(".formShop").on("click", "li a",function () {
        // console.log($(this).html());
        var that_name = $(this).html();
        $('.shopName').html(that_name);
        // 店铺信息的临时id
        var shopTempId = $(this).attr("data-shopId");
        // 把当前点击的id绑定给父元素自定义属性
        $(".formShop").attr("data-shopId",shopTempId)

        var areaId = $(".formArea").attr("data-areaId");

        getProduct(shopTempId,areaId);
    });

    $(".formArea").on("click", "li a",function () {
        // console.log($(this).html());
        var that_name = $(this).html();
        $('.areaName').html(that_name);
        // 地区临时id
        var areaTempId = $(this).attr("data-areaId");
        // 把当前点击的id绑定给父元素自定义属性
        $(".formArea").attr("data-areaId",areaTempId)

        var shopId = $(".formShop").attr("data-shopId");

        getProduct(shopId,areaTempId);
    });


    function getShop() {
        $.ajax({
            type: "get",
            dataType: "json",
            url: "http://193.112.55.79:9090/api/getgsshop",
            success: function (res) {
                // console.log(res.result);
                var shopHtml = template("tp_shop",res.result);
                // console.log(shopHtml);
                document.querySelector('.formShop').innerHTML = shopHtml;
            }
        });
    }

    function getArea() {
        $.ajax({
            type: "get",
            dataType: "json",
            url: "http://193.112.55.79:9090/api/getgsshoparea",
            success: function (res) {
                // console.log(res.result);
                var shopHtml = template("tp_area",res.result);
                // console.log(shopHtml);
                document.querySelector('.formArea').innerHTML = shopHtml;
            }
        });
    }

    function getProduct(shopId,areaId) {
        $.ajax({
            type: "get",
            dataType: "json",
            url: "http://193.112.55.79:9090/api/getgsproduct",
            data:{
                shopid: shopId || 0,
                areaid: areaId || 0,
            },
            success: function (res) {
                // console.log(res.result);
                var productHtml = template("tp_product",res.result);
                // console.log(productHtml);
                document.querySelector('.product_list').innerHTML = productHtml;
            }
        });
    }

});